import util from '../../../utils/util';
import { postAjax } from '../../../utils/ajax';
import { navigate, tip } from '../../../utils/common';

const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data : {
    // 等级为1时不显示推广模块
    promotion : 1,
    //
    modules : [ {
      label : '订单管理',
      url : '/pages/business/order-manage/order-manage'
    }, {
      label : '商品管理',
      url : '/pages/business/goods-manage/goods-manage'
    }, {
      label : '客户管理',
      url : '/pages/business/user-manage/user-manage'
    }, {
      label : '我要推广',
      url : '/pages/business/promote/promote'
    }, {
      label : '账户明细',
      url : '/pages/myWallet/myWallet?b=1'
    }, {
      label : '社区团购设定',
      url : '/pages/business/set-store/set-store'
    }, {
      label : '店铺模板',
      url : '/pages/business/shop-template/shop-template'
      // /pages/business/menber-car/menber-car
    }, {
      label : '价格模板',
      url : '/pages/business/price-template/price-template'
    }, {
      label : '创建直播',
      url : '/pages/business/create-live/create-live'
    }, {
      label : '优惠券管理',
      url : '/pages/business/coupon/coupon'
    }, {
      label : '分享店铺',
      url : ''
    }, {
      label : '售后订单',
      url : '/pages/business/order-manage/order-manage?b=1'
    } ],
    sales : [
      [ '今日浏览用户', 0, '昨日浏览用户', 0, '总浏览量', 0 ],
      [ '今日新增用户', 0, '昨日增加用户', 0, '总用户数', 0 ],
      [ '今日新增订单', 0, '昨日增加订单', 0, '总订单数', 0 ],
      [ '今日拼团销售额', 0, '昨日销售额', 0, '销售总额', 0 ],
      [ '今日利润', 0, '昨日利润', 0, '总利润', 0 ],
      [ '我的余额', 0, '可提现金额', 0, '去提现' ]
    ],
  },
  // shop-header组件查询的数据
  onUpdate(e) {
    this.setData({ promotion : e.detail.promotion });
  },
  //
  onNavigate(e) {
    navigate(`/pages/business/promote-detail/promote-detail?type=${e.detail.type}`);
  },
  // 获取销售数据
  getSaleData() {
    postAjax('/manage/data', {
      bdmin_user_id : util.getBdmin_user_id()
    }).then(data => {
      this.setData({
        __saleData__ : data,
        ['sales[0][1]'] : data.visitData.todayNum,
        ['sales[0][3]'] : data.visitData.yesterdayNum,
        ['sales[0][5]'] : data.visitData.allNum,

        ['sales[1][1]'] : data.customerData.todayNum,
        ['sales[1][3]'] : data.customerData.yesterdayNum,
        ['sales[1][5]'] : data.customerData.allNum,

        ['sales[2][1]'] : data.orderNum.todayNum,
        ['sales[2][3]'] : data.orderNum.yesterdayNum,
        ['sales[2][5]'] : data.orderNum.allNum,

        ['sales[3][1]'] : `￥${data.money.todayMoney}`,
        ['sales[3][3]'] : `￥${data.money.yesterdayMoney}`,
        ['sales[3][5]'] : `￥${data.money.allMoney}`,

        ['sales[4][1]'] : `￥${data.profit.todayProfit}`,
        ['sales[4][3]'] : `￥${data.profit.yesterdayProfit}`,
        ['sales[4][5]'] : `￥${data.profit.allProfit}`,

        ['sales[5][1]'] : `￥${data.wallet.allowance}`,
        ['sales[5][3]'] : `￥${data.wallet.available_money}`

      });
    });
  },
  // 点击销售统计
  saleHandle(e) {
    const index = Number(e.currentTarget.dataset.index);
    switch ( index ) {
      case 0:
        navigate('/pages/business/user-manage/user-manage?type=3');
        break;
      case 1:
        navigate('/pages/business/user-manage/user-manage?type=2');
        break;
      case 2:
        navigate('/pages/business/order-manage/order-manage?day=0');
        break;
      case 3:
        navigate('/pages/business/order-manage/order-manage?type=3');
        break;
      case 4:
        navigate('/pages/business/profit/profit');
        break;
      case 5:
        const money = this.data.__saleData__ && this.data.__saleData__.wallet.available_money;
        if ( Number(money) > 0 ) {
          navigate(`/pages/withdraw/withdraw?total=${money}&id=${this.data.saleData.wallet.id}`);
        } else {
          tip('当前没有可提现金额');
        }
        break;
    }
  },
  // 查看余额明细
  showDetail() {
    wx.navigateTo({
      url : '/pages/myWallet/myWallet?b=1'
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad : function (options) {
    // 分享进来的
    if ( options.share == 1 ) {
      util.stat(app, 'pages/business/index/index', 2);
    } else {
      util.stat(app, 'pages/business/index/index', 1);
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow : function () {
    const shopHeader = this.selectComponent('.shop-header');
    shopHeader.getShopInfo();
    this.getSaleData();
  },
  onShareAppMessage : async function () {
    let that = this;
    var share_img = await new Promise((resolve, reject) => {
      wx.request({
        url : app.globalData.urls + '/order-manage/share-store',
        method : 'POST',
        data : {
          bdmin_user_id : util.getBdmin_user_id()
        },
        header : app.getRequestHeader(),
        success(res) {
          resolve(res.data.data);
        }
      });
    });

    return {
      title : '欢迎光临[' + share_img.store_name + ']',
      desc : '欢迎光临[' + share_img.store_name + ']',
      imageUrl : share_img.store_pic,//share_img.share_img ,  //用户分享出去的自定义图片大小为5:4,
      path : '/pages/index/index?share=1&getBdmin_user_id=' + util.getBdmin_user_id(),
      success : function (res) {
        wx.showToast({
          title : '转发成功',
          icon : 'success'
        });
        // 转发成功
      },
      fail : function (res) {
        wx.showToast({
          title : '转发失败'
        });
        // 转发失败
      }
    };
  }
});
